package com.bookstore.controller;

import com.alibaba.fastjson.JSON;
import com.bookstore.entity.Book;
import com.bookstore.entity.PageBean;
import com.bookstore.entity.User;
import com.bookstore.entity.UserMessage;
import com.bookstore.service.UserMessageService;
import com.bookstore.service.UserService;
import com.bookstore.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author Cofe
 * @Date 2022/4/14 10:35
 * 实现页面的跳转
 */
@Controller
@RequestMapping("/user")
public class UserController {//此类为控制器,类似于Servlet
    @Resource
    private UserService userService;
    @Resource
    private UserMessageService ums;

    @RequestMapping(
            value = "/login",
            method = RequestMethod.POST,
            produces = {"application/text;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    public String UserLogin(String username,String password,HttpSession httpSession){
        User user=userService.findUser(username, password);
        if(user!=null){
            httpSession.setAttribute("user_uid",user.getUid());  //将用户的uid存储到session会话中,方便使用时取出
            httpSession.setAttribute("username",user.getUsername());  //将用户的username存储到session会话中,方便使用时取出
            return "恭喜您登陆成功";
        }else{
            return "用户名或者密码错误,请检查";
        }
    }

    @RequestMapping(
            value = "/outlog",
            method = RequestMethod.GET,
            produces = {"application/text;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    public String Userout(HttpSession httpSession){
        if(httpSession!=null){
            httpSession.invalidate();       //清空 退出登录
            return "您已退出登陆";
        }else{
            return "系统错误,请稍后重试...";
        }
    }

    @RequestMapping(
            value = "/register",
            method = RequestMethod.POST,
            produces = {"application/text;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    public String UserRegister(String username,String password) {
        List<User> userList=userService.findAllUser();          //查询所有用户
        for (User user : userList) {
            if(user.getUsername().equals(username)){
                return "该账号已经存在,请重新注册";
            }
        }
        //遍历完成代表该账号不存在,可以注册
        userService.saveUser(username, password);
        String name="用户"+username;
        ums.insertuser(username, name);
        return "恭喜您注册成功！";
    }

    //一对一查询用户的所有信息
    @RequestMapping(
            value = "/selectusertomsg",
            method = RequestMethod.POST
    )
    @CrossOrigin
    @ResponseBody
    public List<User> selectUserToMsg() {
        List<User> users = userService.selectUserToMessage();
        return users;
    }

    @RequestMapping(
            value = "/selectbypageUser",
            method = RequestMethod.POST,
            produces = {"application/text;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    public String selectByPage(int currentPage,int pageSize) {
        //1. 接收 当前页码 和 每页展示条数    url?currentPage=1&pageSize=5
        //2. 调用service查询
        PageBean<User> pageBean = userService.selectByPageUser(currentPage, pageSize);
        //2. 转为JSON
        String jsonString = JSON.toJSONString(pageBean);
        //3. 写数据
        return jsonString;
    }

    @RequestMapping(
            value = "/updateuser",
            method = RequestMethod.POST,
            produces = {"application/text;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    public String updateUser(int uid,String username, String password, String name, String address, String phone,String mail) {
        if(!password.equals("")) {
            System.out.println("密码为:"+password);
            userService.updatePass(username, password);          //通过用户名更改密码
        }
        UserMessage usermessage = ums.findByUsername(username);
        usermessage.setName(name);
        usermessage.setAddress(address);
        usermessage.setPhone(phone);
        usermessage.setMail(mail);
        String s = ums.updateuser(usermessage.getUsername(), usermessage.getName(), usermessage.getAddress(), usermessage.getGender(), usermessage.getAge(), usermessage.getMail(), usermessage.getPhone());
        return s;
    }


}

